import axios from "axios";
import Cache from "@/utils/Cache";
const myRequest = axios.create({
  baseURL: "http://localhost:8000",
  timeout: 5000,
});
//配置请求拦截器：为每个请求添加token
myRequest.interceptors.request.use((config) => {
  const token = Cache.getItem("token");
  //如果在本地已经存储了token，则将所有请求头都添加上token
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});
class Request {
  get(url, params) {
    return myRequest({
      url,
      method: "GET",
      params,
    });
  }

  post(url, data) {
    return myRequest({
      url,
      method: "POST",
      data,
    });
  }
}

export default new Request();
